using UnityEngine;
using QFramework;
using Unity.Burst.Intrinsics;
using QAssetBundle;
using UnityEngine.EventSystems;

namespace ProjectSurvivor
{
	public partial class Player : ViewController
	{
		public static Player Default;
		public float movementSpeed = 5;

		void Awake()
		{
			Default = this;
		}

		void OnDestroy()
		{
			Default = null;
		}

		void Start()
		{
			// Code Here

			HurtBox.OnTriggerEnter2DEvent((collider2D) =>
			{
				var hitBox = collider2D.GetComponent<HitBox>();
				if (hitBox)
				{
					if (hitBox.Ower.CompareTag("Enemy"))
					{
						Global.HP.Value--;
						if (Global.HP.Value <= 0)
						{
							AudioKit.PlaySound("Die");
							this.DestroyGameObjGracefully();

							UIKit.OpenPanel<UIGameOverPanel>();
						}
						else
						{
							AudioKit.PlaySound("Hurt");
						}

					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			void UpdateHp()
			{
				HpValue.fillAmount=Global.HP.Value/(float)Global.MaxHp.Value;
			}
			Global.HP.RegisterWithInitValue(hp =>
				{
					UpdateHp();
				}).UnRegisterWhenGameObjectDestroyed(gameObject);

				Global.MaxHp.RegisterWithInitValue(maxhp =>
				{
					UpdateHp();
				}).UnRegisterWhenGameObjectDestroyed(gameObject);

		}

		void Update()
		{
			float horizontal = Input.GetAxisRaw("Horizontal");
			float vertical = Input.GetAxisRaw("Vertical");
			var targeVelocity = new Vector2(horizontal, vertical).normalized * movementSpeed;

			SelfRigidbody2D.velocity = Vector2.Lerp(SelfRigidbody2D.velocity, targeVelocity, 1 - Mathf.Exp(-Time.deltaTime * 5));
		}
	}
}
